package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.AccountRoleVO;
import com.ruoyi.system.domain.vo.OperationMenuItemVO;
import com.ruoyi.system.domain.vo.OperationMenuVO;
import com.ruoyi.system.mapper.*;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IGwOperationRoleService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 运营角色Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-08
 */
@Service
public class GwOperationRoleServiceImpl implements IGwOperationRoleService 
{
    @Resource
    private GwOperationRoleMapper gwOperationRoleMapper;
    @Resource
    private GwOperationRoleMenuMapper gwOperationRoleMenuMapper;
    @Resource
    private SysMenuMapper sysMenuMapper;
    @Resource
    private GwOperationAccountRoleMapper accountRoleMapper;
    @Resource
    private GwOperationAccountMapper accountMapper;

    /**
     * 查询运营角色
     * 
     * @param id 运营角色主键
     * @return 运营角色
     */
    @Override
    public GwOperationRole selectGwOperationRoleById(String id)
    {
        GwOperationRole role = gwOperationRoleMapper.selectGwOperationRoleById(id);
        GwOperationRoleMenu roleMenu = new GwOperationRoleMenu();
        roleMenu.setOperationRoleId(id);
        List<GwOperationRoleMenu> roleMenuList = gwOperationRoleMenuMapper.selectGwOperationRoleMenuList(roleMenu);
        if(!CollectionUtils.isEmpty(roleMenuList)){
            List<Long> menuList = roleMenuList.stream().map(GwOperationRoleMenu::getMenuId).collect(Collectors.toList());
            role.setMenuList(menuList);
        }
        return role;
    }

    /**
     * 查询运营角色列表
     * 
     * @param gwOperationRole 运营角色
     * @return 运营角色
     */
    @Override
    public List<GwOperationRole> selectGwOperationRoleList(GwOperationRole gwOperationRole)
    {
        List<GwOperationRole> roleList = gwOperationRoleMapper.selectGwOperationRoleList(gwOperationRole);
        roleList.forEach(k->{
            GwOperationAccountRole accountRole = new GwOperationAccountRole();
            accountRole.setOperationRoleId(k.getId());
            List<GwOperationAccountRole> accountRoleList = accountRoleMapper.selectGwOperationAccountRoleList(accountRole);
            k.setUserCount(accountRoleList.size());
        });
        return roleList;
    }

    /**
     * 新增运营角色
     * 
     * @param gwOperationRole 运营角色
     * @return 结果
     */
    @Override
    public int insertGwOperationRole(GwOperationRole gwOperationRole)
    {
        gwOperationRole.setId(IdUtils.fastSimpleUUID());
        gwOperationRole.setCreateBy(SecurityUtils.getUserId()+"");
        gwOperationRole.setCreateTime(DateUtils.getNowDate());
        int num = gwOperationRoleMapper.insertGwOperationRole(gwOperationRole);
        this.saveRoleMenu(gwOperationRole);
        return num;
    }

    /**
     * 修改运营角色
     * 
     * @param gwOperationRole 运营角色
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateGwOperationRole(GwOperationRole gwOperationRole)
    {
        gwOperationRole.setUpdateBy(SecurityUtils.getUserId()+"");
        gwOperationRole.setUpdateTime(DateUtils.getNowDate());
        int num = gwOperationRoleMapper.updateGwOperationRole(gwOperationRole);
        //先删除后插入
        GwOperationRoleMenu roleMenu = new GwOperationRoleMenu();
        roleMenu.setOperationRoleId(gwOperationRole.getId());
        List<GwOperationRoleMenu> menus = gwOperationRoleMenuMapper.selectGwOperationRoleMenuList(roleMenu);
        if(!CollectionUtils.isEmpty(menus)){
            menus.forEach(k->{
                gwOperationRoleMenuMapper.deleteGwOperationRoleMenuByOperationRoleId(k.getOperationRoleId());
            });
        }
        this.saveRoleMenu(gwOperationRole);

        return num;
    }

    private void saveRoleMenu(GwOperationRole gwOperationRole){
        if(!CollectionUtils.isEmpty(gwOperationRole.getMenuList())){
            gwOperationRole.getMenuList().forEach(k->{
                GwOperationRoleMenu vo = new GwOperationRoleMenu();
                vo.setMenuId(k);
                vo.setOperationRoleId(gwOperationRole.getId());
                gwOperationRoleMenuMapper.insertGwOperationRoleMenu(vo);
            });
        }
    }

    /**
     * 批量删除运营角色
     * 
     * @param ids 需要删除的运营角色主键
     * @return 结果
     */
    @Override
    public int deleteGwOperationRoleByIds(String[] ids)
    {
        return gwOperationRoleMapper.deleteGwOperationRoleByIds(ids);
    }

    /**
     * 删除运营角色信息
     * 
     * @param id 运营角色主键
     * @return 结果
     */
    @Override
    public int deleteGwOperationRoleById(String id)
    {
        return gwOperationRoleMapper.deleteGwOperationRoleById(id);
    }

    @Override
    public List<OperationMenuVO> operationMenuList() {
        List<OperationMenuVO> result = Lists.newArrayList();
        SysMenu sysMenu = new SysMenu();
        sysMenu.setMenuName("平台运营管理");
        List<SysMenu> sysMenus = sysMenuMapper.selectMenuList(sysMenu);
        if(!CollectionUtils.isEmpty(sysMenus)){
            sysMenu = sysMenus.get(0);

            SysMenu child = new SysMenu();
            child.setParentId(sysMenu.getMenuId());
            List<SysMenu> childMenuList = sysMenuMapper.selectMenuList(child);
            if(!CollectionUtils.isEmpty(childMenuList)){
                for(SysMenu sm : childMenuList){
                    OperationMenuVO  menuVO = new OperationMenuVO();
                    menuVO.setId(sm.getMenuId());
                    menuVO.setMenuName(sysMenu.getMenuName()+"-"+sm.getMenuName());
                    SysMenu sun = new SysMenu();
                    sun.setParentId(sm.getMenuId());
                    List<SysMenu> sunList = sysMenuMapper.selectMenuList(sun);
                    if(!CollectionUtils.isEmpty(sunList)){
                        List<OperationMenuItemVO> itemList = Lists.newArrayList();
                        for(SysMenu item:sunList){
                            OperationMenuItemVO vo = new OperationMenuItemVO();
                            vo.setId(item.getMenuId());
                            vo.setMenuName(item.getMenuName());
                            itemList.add(vo);
                        }
                        menuVO.setItemList(itemList);
                    }
                    result.add(menuVO);
                }
            }
        }

        return result;
    }

    @Override
    public List<GwOperationAccount> accountListByRole(GwOperationRole gwOperationRole) {
        return accountMapper.accountListByRole(gwOperationRole.getId());
    }

    @Override
    public int deleteAccountFormRole(AccountRoleVO accountRoleVO) {
        int num = 0;
        GwOperationAccountRole accountRole = new GwOperationAccountRole();
        accountRole.setAccountId(accountRoleVO.getAccountId());
        accountRole.setOperationRoleId(accountRoleVO.getRoleId());
        List<GwOperationAccountRole> roleList = accountRoleMapper.selectGwOperationAccountRoleList(accountRole);
        if(!CollectionUtils.isEmpty(roleList)){
            num = accountRoleMapper.deleteGwOperationAccountRoleById(roleList.get(0).getId());
        }
        return num;
    }

    @Override
    public Collection<String> selectRolePermissionByUserId(Long userId) {
        List<GwOperationRole> perms = accountRoleMapper.selectRolePermissionByUserId(userId);
        Set<String> permsSet = new HashSet<>();
        for (GwOperationRole perm : perms)
        {
            if (StringUtils.isNotNull(perm))
            {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
    }
}
